var versionNum = 3;

function checkUpdate(){
    $("#checkUpdateDiv").hide();

    var versionUrl = "https://gitee.com/ccpp/copy-block-breaker/raw/master/js/version.json";

    var xhr = new XMLHttpRequest();
    xhr.open("GET", versionUrl, true);
    xhr.onreadystatechange = function() {
        if (xhr.readyState == 4) {
            html_text = xhr.responseText;

            if(html_text && html_text.indexOf("serverVersion") > -1){
                var versionJson = JSON.parse(html_text);

                var serverVersion = versionJson["serverVersion"];

                if(versionNum == serverVersion) {
                    //已是最新
                    chrome.browserAction.setBadgeText({text: ''});
                    chrome.browserAction.setBadgeBackgroundColor({color: [0, 0, 0, 0]});
                    $("#checkUpdateDiv").html("已是最新版");
                    $("#checkUpdateDiv").show();

                    $("#checkUpdateDiv").css("color", "#0000FF");
                }else{
                    //有更新
                    chrome.browserAction.setBadgeText({text: 'new'});
                    chrome.browserAction.setBadgeBackgroundColor({color: [255, 0, 0, 255]});
                    $("#checkUpdateDiv").html("有更新");
                    $("#checkUpdateDiv").show();

                    $("#checkUpdateDiv").css("color", "#FF0000");
                }

            }

        }
    }
    xhr.send();

}

$(document).ready(function(){
    try {
        checkUpdate();
    } catch (e) {
    }
});
